using Glpt.Common;
using Glpt.Task;
using System;

namespace GTask
{
    /// <summary>
    /// 前提条件检测
    /// </summary>
    public class CheckPremise
    {
        /// <summary>
        /// 初始化类的新实例
        /// </summary>
        public CheckPremise()
        {
        }

        /// <summary>
        /// 参数设置
        /// </summary>
        /// <param name="tradedate">业务日期</param>
        /// <param name="tradetime">业务时间</param>
        /// <param name="task_function_id">任务号</param>
        public int SetParams(string tradedate, string tradetime, string task_function_id)
        {
            return SetParams(tradedate, tradetime, task_function_id, "");
        }
        /// <summary>
        /// 参数设置
        /// </summary>
        /// <param name="tradedate">业务日期</param>
        /// <param name="tradetime">业务时间</param>
        /// <param name="task_function_id">任务号</param>
        /// <param name="check_type_id">检测类型或顺序号</param>
        public int SetParams(string tradedate, string tradetime, string task_function_id, string check_type_id)
        {
            return SetParams(tradedate, tradetime, task_function_id, check_type_id, "0");
        }

        /// <summary>
        /// 参数设置
        /// </summary>
        /// <param name="tradedate">业务日期</param>
        /// <param name="tradetime">业务时间</param>
        /// <param name="task_function_id">任务号</param>
        /// <param name="check_type_id">检测类型或顺序号</param>
        /// <param name="use_flag">检测是否使用</param>
        public int SetParams(string tradedate, string tradetime, string task_function_id, string check_type_id, string use_flag)
        {
            int isover = 0;
            try
            {
                if (Funcs.CheckTime(tradedate, tradetime))
                {
                    string check_type = "";
                    string check_id = "";
                    int idx = check_type_id.IndexOf(":");
                    if (idx >= 0)
                    {
                        check_type = check_type_id.Substring(0, idx).ToLower();
                        check_id = check_type_id.Substring(idx + 1);
                    }
                    else
                    {
                        int itmp = 0;
                        if (int.TryParse(check_type_id, out itmp))
                            check_id = check_type_id;
                        else
                            check_type = check_type_id.ToLower();
                    }
                    bool check_use = use_flag.Equals("1");
                    if (check_type.Equals("copy"))
                    {
                        string sql = "SELECT SOURCE_PATH,DATE_EXP FROM TB_COPY_LIST WHERE TASK_FUNCTION_ID=" + task_function_id + " AND ENABLE_FLAG='1'";
                        if (check_id.Trim().Length > 0) sql += " AND COPY_ID=" + check_id;
                        isover = FilePath.CheckFile(sql, tradedate, check_use);
                    }
                    else if (check_type.Equals("exch"))
                    {
                        string sql = "SELECT A.SOURCE_TABLE_NAME,A.DATE_EXP,COALESCE(B1.DB_IP,B2.DB_IP) AS PATH1,COALESCE(B1.DB_NAME,B2.DB_NAME) AS PATH2 FROM TB_EXCH_LIST A LEFT JOIN TB_DATABASE B1 ON A.SOURCE_DB_NO=B1.DB_NO AND A.SOURCE_DB_LIST='0' AND B1.DB_TYPE='9' LEFT JOIN TB_EXCH_DATABASE B2 ON A.SOURCE_DB_NO=B2.DB_NO AND A.SOURCE_DB_LIST='1' AND B2.DB_TYPE='9' WHERE A.TASK_FUNCTION_ID=" + task_function_id + " AND A.ENABLE_FLAG='1' AND COALESCE(B1.DB_NO,B2.DB_NO) IS NOT NULL";
                        if (check_id.Trim().Length > 0) sql += " AND A.EXCH_ID=" + check_id;
                        isover = FilePath.CheckFile(sql, tradedate, check_use);
                    }
                    else if (check_type.Equals("import"))
                    {
                        string sql = "SELECT SOURCE_FILE,DATE_EXP FROM TB_IMPORT_LIST WHERE TASK_FUNCTION_ID=" + task_function_id + " AND ENABLE_FLAG='1'";
                        if (check_id.Trim().Length > 0) sql += " AND IMPORT_ID=" + check_id;
                        isover = FilePath.CheckFile(sql, tradedate, check_use);
                    }
                    else
                    {
                        CheckTask task = new CheckTask();
                        isover = task.Check(tradedate, task_function_id, check_id, check_use);
                    }
                }
            }
            catch
            {
                isover = 0;
            }
            return isover;
        }

        /// <summary>
        /// 释放内存
        /// </summary>
        public void Dispose()
        {
            try
            {
                GC.Collect();
            }
            catch { }
        }

        /// <summary>
        /// 析构函数
        /// </summary>
        ~CheckPremise()
        {
            Dispose();
        }
    }
}
